package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.CertificationManageMapper;
import com.example.demo.mapper.StudentAttributeMapper;
import com.example.demo.mapper.StudentPointsMapper;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.CertificationManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CertificationManageServiceImpl extends ServiceImpl<CertificationManageMapper, StudentCertification> implements CertificationManageService {

    @Autowired
    private CertificationManageMapper mapper;

    @Autowired
    private StudentPointsMapper studentPointsMapper;

    @Autowired
    private StudentAttributeMapper studentAttributeMapper;

    @Override
    public PageVo<StudentCertification> getCertificationList(PageBo pageBo) {
        List<StudentCertification> logs = mapper.getCertificationList();
        return new PageVo<>(pageBo, logs);
    }

    @Override
    public PageVo<StudentCertification> searchCertification(String studentNum, PageBo pageBo) {
        List<StudentCertification> list = mapper.getCertificationList();
        //按照学号搜索
        list = list.stream()
                .filter(o -> (studentNum == null || o.getStudentNumber().contains(studentNum))).collect(Collectors.toList());
        return new PageVo<>(pageBo, list);
    }

    @Override
    public StudentCertification getByNumber(String studentNumber) {
        return mapper.getByNumber(studentNumber);
    }

    @Override
    public void approveStudent(StudentCertification student) {
        String number=student.getStudentNumber();
        int id = mapper.getIdByNumber(number);
        StudentPoints points = new StudentPoints();
        //在studentPoints表中插入数据
        points.setStudentId(id);
        if(studentPointsMapper.selectStudentPointsByStudentId(id)==null){
            studentPointsMapper.insertStudentPoints(points);
        }
        //在studentAttribute表中插入数据
        if(studentAttributeMapper.getStudentAttributeList(id)==null){
            List<Attribute> attributeList = studentAttributeMapper.selectAllAttribute();
            StudentAttribute studentAttribute = new StudentAttribute();
            studentAttribute.setStudentId(id);
            for (Attribute attribute : attributeList) {
                studentAttribute.setAttributeId(attribute.getAttributeId());
                studentAttributeMapper.insertStudentAttribute(studentAttribute);
            }
        }
        //修改student表状态
        mapper.updateStudentState(id);
        //修改studentCertification表状态
        StudentCertification curStudent=new StudentCertification();
        curStudent.setStudentNumber(number);
        curStudent.setState(3);
        curStudent.setId(student.getId());
        mapper.updateById(curStudent);

    }

    @Override
    public void disapproveStudent(int id, int disapproveReason) {
        StudentCertification curStudent=new StudentCertification();
        curStudent.setId(id);
        curStudent.setDisapproveReason(disapproveReason);
        if(curStudent.getId()!=0){
            curStudent.setState(4);
            mapper.updateById(curStudent);
        }
    }

    @Override
    public int getIdByNumber(String number) {
        return mapper.getIdByNumber(number);
    }

    @Override
    public String getStudentName(int id) {
        return mapper.getStudentName(id);
    }
}
